package apig.client.stud;

import java.util.*;

import apig.client.*;
import apig.client.serviface.*;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.user.client.ui.*;

public class StudContentPanel extends HorizontalPanel {

	private UserData ud;
	private TextArea codeBrowser;
	private VerticalPanel vpanel;
	private Lab currentLab;

	private MouseDownHandler mdownh = new MouseDownHandler() {

		@Override
		public void onMouseDown(MouseDownEvent event) {
			((CodeLinkWidget) event.getSource()).onCodeSelected();
		}
	};
	private MouseOverHandler moverh = new MouseOverHandler() {

		@Override
		public void onMouseOver(MouseOverEvent event) {
			((CodeLinkWidget) event.getSource()).setHighlighted(true);
		}
	};
	private MouseOutHandler mouth = new MouseOutHandler() {

		@Override
		public void onMouseOut(MouseOutEvent event) {
			((CodeLinkWidget) event.getSource()).setHighlighted(false);
		}
	};

	public StudContentPanel(UserData ud) {
		this.ud = ud;
		setBorderWidth(2);
	}

	public void setLabInBrowser(Lab lab) {
		currentLab = lab;
		// set some loading info
		;
		//
		CodeQuery cq = new CodeQuery();
		cq.labId = lab.labId;
		cq.ownerId = ud.userId;
		cq.revision = CodeQuery.LAST_REVISION;
		Stubman.getStubman().getService()
				.getCodes(cq, new AsyncCallback<List<Code>>() {

					@Override
					public void onFailure(Throwable caught) {
						onLoadLabFail(caught.getMessage());
					}

					@Override
					public void onSuccess(List<Code> result) {
						onLoadLabSuccess(result);
					}
				});
	}
	
	public void reloadContent() {
		if (currentLab == null) {
			throw new IllegalStateException();
		} else {
			setLabInBrowser(currentLab);
		}
	}

	private void onLoadLabFail(String msg) {
		System.out.println("fail: " + msg);
	}

	private void onLoadLabSuccess(List<Code> codes) {
		this.clear();
		vpanel = new VerticalPanel();

		VerticalPanel vpanel = new VerticalPanel();
		codeBrowser = new TextArea();
		codeBrowser.setWidth("100%");
		codeBrowser.setHeight("100%");

		// create code list
		FlowPanel fp = new FlowPanel();
		fp.setWidth("100%");
		vpanel.setCellWidth(fp, "100%");
		for (Code code : codes) {
			fp.add(new CodeLinkWidget(code));
		}
		vpanel.add(fp);
		vpanel.add(new HTML("<hr>"));
		vpanel.add(codeBrowser);
		vpanel.setCellWidth(codeBrowser, "100%");
		vpanel.setCellHeight(codeBrowser, "100%");
		vpanel.setWidth("100%");
		vpanel.setHeight("100%");
		
		setCellWidth(vpanel, "100%");
		setCellHeight(vpanel, "100%");
		add(vpanel);
	}

	private class CodeLinkWidget extends Button {

		private Code c;

		public CodeLinkWidget(Code c) {
			this.c = c;
			addMouseOverHandler(moverh);
			addMouseOutHandler(mouth);
			addMouseDownHandler(mdownh);
			setText(c.name);
			setHighlighted(false);
		}

		public void setHighlighted(boolean b) {
			if (b) {
				setStyleName("selected_link_widget");
			} else {
				setStyleName("deselected_link_widget");
			}
		}

		public void onCodeSelected() {
			codeBrowser.setText("rev: " +c.revision + "owner: " + c.ownerId + ";" + c.text);
		}
	}
}